"""
从百度获取北京时间，如获取失败，则返回None
"""
import time
import http.client


def getBeijinTime():
    """
　　 获取北京时间
    """
    try:
        conn = http.client.HTTPConnection("www.baidu.com")
        conn.request("GET", "/")
        r = conn.getresponse()
        if r.status == 200:
            ts = r.getheader('date')  # 获取http头date部分
            # 将GMT时间转换成北京时间
            ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
            # print(ltime)
            ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)  # 转换为北京时间
            # print(ttime)
            dat = "%u%02u%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
            # tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)

        print(dat)
        return dat
    except:
        print("getBeijinTime except")
        return None

# getBeijinTime()
